<?php

//error_reporting(0);
function array_length($arr){
    $length = 0;
    foreach($arr as $key =>$value){
            $length++;
    }
    return $length;
}
function array_implode($arr,$needle=','){
    $length = array_length($arr);
    $str = $needle;
    $i = 0;
    while($i<$length){
        $inst = $arr[$i]; 
        //$str .=$interests[$i];
        $str = $str . $inst.$needle;
        $i++;
    }
    return $str;
}
$hasError=false; 

//检查用职工号
//必须填写
//字母开头,字母数字下划线
$tnumber = trim($_POST["Tnumber"]);
if(!$tnumber) {
    echo "职工号必须填写<br/>";
    $hasError = true;
}else {
    $len = strlen($tnumber);
    if($len<3){
        echo "职工号至少要有三个字符<br/>";
        $hasError = true;
    }else if($len>9){
        echo "职工号最多九个字符";
        $hasError = true;
    }/*else if(!preg_match('/^[a-zA-Z0-9_]*$/',$tnumber)){
        echo "格式不符合要求";
        $hasError = true;*/
    
}


//检查密码
//必须填写

$password = trim($_POST["Password"]);
if(!$password) {
    echo "密码必须填写<br/>";
    $hasError=true;
}else {
    $len = strlen($password);
    if($len<3){
        echo "密码至少三个字符<br/>";
        $hasError = true;
    }else if($len>30){
        echo "密码最多三十个字符<br/>";
        $hasError = true;
    }
}

//姓名检查
$name = trim($_POST["Name"]);
if(!$name) {
    echo "姓名必须填写<br/>";
    $hasError = true;
}else {
    $len = strlen($name);
    if($len<1){
        echo "姓名至少要有一个字符<br/>";
        $hasError = true;
    }else if($len>20){
        echo "姓名最多五个字符";
        $hasError = true;
    }/*else if(!preg_match('/^[a-zA-Z0-9_]*$/',$name)){
        echo "格式不符合要求";
        $hasError = true;*/
    
}




//性别
$gender = $_POST['Gender'];
if($gender == 'male') $gender= "男";
else if($gender =='female') $gender = "女";
else $gender = -1;


//身份证
$idnumber = $_POST['IDNumber'];
if($idnumber){
    // '/^\d+$/'
    $IDNum = intval($idnumber);
    if(!$IDNum || $IDNum <=0 || $IDNum>999999999999999999){
        echo "请填写正确的身份证号码<br/>";
        $hasError = true;
    }

}

//学院
$department = $_POST["Department"];
$length = array_length($department);
if($length<1){
    echo"学院必须选择<br/>";
    $hasError = true;
}

//专业
$profession = $_POST["Profession"];
$length = array_length($profession);
if($length<1){
    echo"专业必须选择<br/>";
    $hasError = true;
}

//班级
$class = $_POST["Class"];
if(!$class){
    echo"班级必须填写<br/>";
    $hasError = true;
}


$departmentStr = array_implode($department,"|");
$professionStr = array_implode($profession,"|");
//$gender = array_implode($gender,"|");


if(!$hasError){
    $sql = "INSERT INTO `count` (`userid`, `password`,`name`,`gender`,`sfzinformation`,`xy`,`zy`,`classno`)VALUES('$tnumber','$password',
    '$name','$gender','$idnumber','$departmentStr','$professionStr','$class')";
    echo $sql;

    $sqli = new mysqli("localhost","root","","mybolg");
    $sqli ->query($sql);
    echo "注册成功！<br/>";
}

if(!$hasError) echo "检查成功<br/>";


echo "<a href='login.text.html'>返回登录界面</a>";

?>
